package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteChapterOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Size;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VocChapterServiceImpl extends ServiceImpl<VocChapterMapper, VocChapterDO> implements VocChapterService  {
    @Autowired
    VocChapterMapper vocChapterMapper;

    @Resource
    VocChapterConverter vocChapterConverter;
    @Override
    public PageDTO<VocChapterDTO> getChapterPage(VocChapterPageRequest pageQuery) {
        PageDTO<VocChapterDTO> vocChapterDTOPageDTO = null;
        if ("".equals(pageQuery.getKeyword())){
            pageQuery.setKeyword(null);
        }
        if(pageQuery.getPageNum() == null || pageQuery.getCount() == null){
            QueryWrapper<VocChapterDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("book_id",pageQuery.getBookId());
            List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(queryWrapper);
            vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(vocChapterDOS, Long.valueOf(vocChapterDOS.size()));
        }else {
            Page<VocChapterDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
            QueryWrapper<VocChapterDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(pageQuery.getKeyword() != null,"chapter_name", pageQuery.getKeyword())
                    .eq(pageQuery.getBookId() != null,"book_id", pageQuery.getBookId());
            Page<VocChapterDO> selectPage = vocChapterMapper.selectPage(page, queryWrapper);
            List<VocChapterDO> records = selectPage.getRecords();
            long total = selectPage.getTotal();
            vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(records, total);
        }
        return vocChapterDTOPageDTO;
    }

    @Override
    public void addChapter(VocChapterCommand command) {
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        vocChapterMapper.insert(vocChapterDO);
    }

    @Override
    public void updateChapter(VocChapterCommand command) {
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        vocChapterMapper.updateById(vocChapterDO);
    }

    @Override
    public DeleteChapterOperateDTO deleteChapter(Long chapterId) {
        VocChapterDO vocChapterDO = vocChapterMapper.selectById(chapterId);
        if (vocChapterDO == null) {
            return null;
        }
        vocChapterMapper.deleteById(chapterId);
        DeleteChapterOperateDTO deleteChapterOperateDTO = vocChapterConverter.vocChapterDO2DeleteDTO(vocChapterDO);
        return deleteChapterOperateDTO;
    }

    @Override
    public List<DeleteChapterOperateDTO> deleteChapters(BatchDeleteCommand command)     {
        List<String> ids = command.getIds();
        List<VocChapterDO> vocChapterDOS = listByIds(ids);
        if (vocChapterDOS == null) {
            return null;
        }
        List<DeleteChapterOperateDTO> deleteChapterOperateDTOS = vocChapterConverter.vocChapterDOs2DeleteDTOs(vocChapterDOS);
        return deleteChapterOperateDTOS;
    }
}
